public class ThreadDemo18 {
    public static void main(String[] args) {
        Object A = new Object();
        Object B = new Object();
        Thread t1 = new Thread(() ->{
            synchronized (A){
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                synchronized (B){//尝试给B加锁
                    System.out.println("t1 拿到了两把锁");
                }
            }
        });
        Thread t2 = new Thread(() ->{

            //此时只需要都先对A加锁后对B加锁即可
            synchronized (A){
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                synchronized (B){ //尝试给A加锁
                    System.out.println("t2 拿到了两把锁");
                }
            }
        });
        t1.start();
        t2.start();
    }
}
